<template>
  <div class="live">
    <div class="content">
      <div class="top">
        <div class="title-wrapper">
          <div class="title">直播间列表</div>
        </div>
        <el-button type="primary" @click.native="add">创建直播间</el-button>
      </div>
      <div>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="直播中" name="first" />
          <el-tab-pane label="未开始" name="second" />
          <el-tab-pane label="已结束" name="third" />
          <el-tab-pane label="禁播" name="fourth" />
          <el-tab-pane label="暂停中" name="fifth" />
          <el-tab-pane label="异常" name="sixth" />
          <el-tab-pane label="已过期" name="seventh" />
        </el-tabs>
      </div>
    </div>
    <div class="bottom">
      <table-list :list="list" />
    </div>
    <Pagination class="pagination" :total="total" :cur-page="currentPage" @nextPage="handleNextPage" />
  </div>
</template>
<script>
import TableList from '@/views/live/table-list'
import Pagination from '@/common/pagination'
import { getLiveRomeList, updateLiveRome } from '@/api/live'
const ERR_OK = 200
export default {
  components: {
    TableList,
    Pagination
  },
  data() {
    return {
      currentPage: 1,
      status: 101,
      activeName: 'first',
      list: [],
      total: 0
    }
  },
  created() {
    this._updateLiveRome()
      .then(() => {
        this._getLiveRomeList({ page: this.currentPage, status: this.status })
      })
  },
  methods: {
    handleNextPage(page) {
      this.currentPage = page
      this._getLiveRomeList({ page: this.currentPage, status: this.status })
    },
    handleClick(tab, event) {
      this.currentPage = 1
      switch (tab.name) {
        case 'first':
          this.status = 101
          break
        case 'second':
          this.status = 102
          break
        case 'third':
          this.status = 103
          break
        case 'fourth':
          this.status = 104
          break
        case 'fifth':
          this.status = 105
          break
        case 'sixth':
          this.status = 106
          break
        case 'seventh':
          this.status = 107
          break
      }
      this._getLiveRomeList({ page: this.currentPage, status: this.status })
    },
    add() {
      this.$router.push('/live/add-live-rome')
    },
    _getLiveRomeList({ status, page }) {
      getLiveRomeList({ status, page })
        .then(res => {
          if (res.code === ERR_OK) {
            this.list = res.data.data
            this.total = res.data.total
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _updateLiveRome() {
      return updateLiveRome()
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.$message({
              message: '更新成功',
              type: 'success'
            })
            return Promise.resolve()
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/el-button.scss";
@import "@/styles/el-pagination.scss";
.live {
  padding: 0 20px;
  box-sizing: border-box;
  .top {
    width: 100%;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    .title-wrapper {
      display: flex;
      align-content: center;
      .title {
        height: 44px;
        line-height: 44px;
        color: rgba(68, 68, 68, 1);
        font-size: 18px;
        font-family: Roboto;
        font-weight: bold;
        cursor: pointer;
      }
    }
  }
}
</style>
